package com.cpecc.manager.wuzi;

import com.cpecc.dao.wuzi.WeiXiuDao;
import com.cpecc.dao.wuzi.WeiXiuJLDao;
import com.cpecc.liucheng.manager.LiuChengManager;
import com.cpecc.liucheng.model.LiuChengUser;
import com.cpecc.liucheng.manager.HuanJieManager;
import com.cpecc.manager.system.UserManager;
import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.WeiXiu;
import com.cpecc.model.wuzi.WeiXiuJL;
import com.cpecc.model.wuzi.WuZi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WeiXiuManager {

	@Autowired
	private WeiXiuDao weiXiuDao;

	@Autowired
	private WeiXiuJLDao weiXiuJLDao;

	@Autowired
	private HuanJieManager huanJieManager;

	@Autowired
	private LiuChengManager liuChengManager;

	@Autowired
	private UserManager userManager;

	@Transactional
	public void save(WeiXiu weiXiu){
		weiXiuDao.save(weiXiu);
	}

	@Transactional
	public boolean cover(WeiXiu weiXiu){
		return weiXiuDao.cover(weiXiu);
	}

	public WeiXiu get(String id){
		return weiXiuDao.get(id);
	}
	
	public List<WeiXiu> getAllWeiXiuByZT(int zhuangT){
		return weiXiuDao.getAllWeiXiuByZT(zhuangT);
	}
	
	public List<WeiXiu> getMyWeiXiu(User user){
		return weiXiuDao.getMyWeiXiu(user);
	}
	
	public List<WeiXiu> getMyWeiXiuRW(User user){
		return weiXiuDao.getMyWeiXiuRW(user);
	}

	public List<WeiXiu> getMyWeiXiuRW(User user, Integer huanJieId){
		List<WeiXiu> data = new ArrayList<>();

		List<LiuChengUser> list = liuChengManager.list(huanJieId, user.getId());

		if (!CollectionUtils.isEmpty(list)) {
			WeiXiu weiXiu = null;
			for (LiuChengUser item : list) {
				if (item.getChuLiJG() != null && item.getChuLiJG().intValue() == 1) {
					continue;
				}

				weiXiu = weiXiuDao.getByLiuChengId(item.getLiuChengId());
				if (weiXiu == null) {
					continue;
				}

				data.add(weiXiu);
			}
		}
		return data;
	}
	
	public List<WeiXiu> getMyWeiXiuQR(User user){
		return weiXiuDao.getMyWeiXiuQR(user);
	}
	
	public List<WeiXiu> getAllWeiXiuByWuZi(WuZi wuZi){
		return weiXiuDao.getAllWeiXiuByWuZi(wuZi);
	}
	
	public boolean hasWuZi(String wuZiId){
		return weiXiuDao.hasWuZi(wuZiId);
	}
	
	public boolean hasLiuCheng(User user){
		return weiXiuDao.hasLiuCheng(user);
	}
	
	public List<WeiXiu> activeWeiXiu(){
		return weiXiuDao.activeWeiXiu();
	}


	@Transactional
	public void doLiuCheng(String weiXiuId, User currUser) {
		WeiXiu weiXiu = weiXiuDao.get(weiXiuId);
		Integer currHuanJieId = weiXiu.getHuanJie().getId();
		Integer nextHuanJieId = getNextHuanJieId(currHuanJieId);

		weiXiu.setHuanJie(huanJieManager.get(nextHuanJieId));
		cover(weiXiu);

		liuChengManager.updateAndSave(weiXiu.getLiuChengId(), currHuanJieId, nextHuanJieId, currUser.getId(), getHuanJieUsers(nextHuanJieId, currUser));
		weiXiuJL(currHuanJieId, huanJieManager.getHuangJieMC(currHuanJieId), null, weiXiu, currUser);
	}

	private void weiXiuJL(Integer huanJieID, String piFuLX, String piFuNR, WeiXiu weiXiu, User user) {
		WeiXiuJL weiXiuJL=new WeiXiuJL();
		weiXiuJL.setCaoZuoRQ(new Date());
		weiXiuJL.setHuanJie(huanJieManager.get(huanJieID));
		weiXiuJL.setPiFuLX(piFuLX);
		weiXiuJL.setPiFuNR(piFuNR);
		weiXiuJL.setUser(user);
		weiXiuJL.setWeiXiu(weiXiu);
		weiXiuJLDao.save(weiXiuJL);
	}

	private Integer getNextHuanJieId(Integer currentHuanJieId) {
		if (currentHuanJieId == 401) {
			return 408;
		}

		if (currentHuanJieId == 409) {
			return 402;
		}
		return currentHuanJieId + 1;
	}

	private List<User> getHuanJieUsers(Integer huanJieId, User currUser) {
		if (huanJieId == 408) {
			return userManager.getBuMenLingDaos2(currUser);
		}

		if (huanJieId == 409) {
			List<User> glyList = userManager.getGZGLY();
			if (CollectionUtils.isEmpty(glyList)) {
				return null;
			}

			List<User> userList = userManager.getGKBMFZR();

			return userList;
		}
		return null;
	}

	private List<User> mergeUserList(List<User> list0, List<User> list1) {
		if (CollectionUtils.isEmpty(list1)) {
			return list0;
		}

		if (list0 == null) {
			list0 = new ArrayList<>();
		}

		if (list0.isEmpty()) {
			list0.addAll(list1);
		}

		List<User> userList = new ArrayList<>();
		userList.addAll(list0);

		for (User u1 : list1) {
			boolean isAdd = true;
			for (User u0 : list0) {
				if (u0.getId().equals(u1.getId())) {
					isAdd = false;
					break;
				}
			}

			if (isAdd) {
				userList.add(u1);
			}
		}
		return userList;
	}

}
